package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.estoque.ItemEstoque;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = ItemEstoque.class)
public class ItemEstoqueConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringIdItemEstoque) {

		if ((stringIdItemEstoque == null)
				|| (stringIdItemEstoque.trim().length() == 0))
			return null;

		Long idItemEstoque = Long.valueOf(stringIdItemEstoque);
		return this.serviceFactory.getEstoqueService()
				.consultarItemEstoquePorId(idItemEstoque);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objItemEstoque) {
		if (objItemEstoque == null)
			return null;
		ItemEstoque itemEstoque = (ItemEstoque) objItemEstoque;
		return String.valueOf(itemEstoque.getIdItemEstoque());
	}

}
